package com.gopawpaw.android.demo.views;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.gopawpaw.android.demo.R;

/**
 * 该Demo显示进度条的基本使用
 * @author Jason
 * @since: 2011-5-16
 */
public class ProgressBarActivity extends Activity {
	private static final String  TAG = "ProgressBarActivity";
	
	private ProgressBar progressHorizontal = null;
	private SeekBar seekBar = null;
	private Thread  mTread = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //设置布局
        setContentView(R.layout.layout_progressbar);
        
        //获取拖动条对象
        seekBar = (SeekBar)findViewById(R.id.seekBar);
        
        //设置拖动条进度改变事件
        seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
        
        //设置进度最大值
        seekBar.setMax(100);
        
        //获取进度条对象
        progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
        
        //设置进度条最大值
        progressHorizontal.setMax(100);
        
        mTread = new Thread(runable);
        mTread.start();
        
    }
    
    //当前进度
    int currentProgress = 0;
    Runnable runable = new Runnable(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
			
			while(true){
				for(currentProgress=0;currentProgress<=progressHorizontal.getMax();currentProgress++){
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					//第一个进度
					progressHorizontal.setProgress(currentProgress);
					
					//第二个进度
					progressHorizontal.setSecondaryProgress(currentProgress+10);
					
					seekBar.setProgress(currentProgress);
					
				}
			}
		}
    	
    };
    
    private OnSeekBarChangeListener onSeekBarChangeListener = new OnSeekBarChangeListener(){

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			//拖动条拖动进行时响应事件
			Log.d(TAG, "onProgressChanged progress:"+progress);
			
			//改变当前进度
			currentProgress = progress;
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			//拖动条拖动开始时响应事件
			Log.d(TAG, "onStartTrackingTouch progress:"+seekBar.getProgress());
			
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			//拖动条拖动停止时响应事件
			Log.d(TAG, "onStopTrackingTouch progress:"+seekBar.getProgress());
		}
    	
    };
}